package com.lizard.schedule.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

import com.lizard.schedule.ScheduleApplication;

/**
 * 设备相关工具类
 */
public class DeviceUtil{

    /**
     * 屏幕像素宽度
     */
    private static int screenWidth;

    /**
     * 屏幕像素高度
     */
    private static int screenHeight;

    private static String imei;

    public static void init(){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        displayMetrics = ScheduleApplication.applicationContext.getApplicationContext().getResources().getDisplayMetrics();
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;

        TelephonyManager telephonyManager = (TelephonyManager) ScheduleApplication.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
        imei = telephonyManager.getDeviceId();
    }

    /**
     * 获取屏幕像素宽度
     *
     * @return
     */
    public static int getScreenWidth(){
        return screenWidth;
    }

    /**
     * 获取屏幕像素高度
     *
     * @return
     */
    public static int getScreenHeight(){
        return screenHeight;
    }

    /**
     * 获取imei
     *
     * @return
     */
    public static String getImei(){
        return imei;
    }

    /**
     * 获取imsi
     *
     * @return
     */
    public static String getImsi(){
        TelephonyManager telephonyManager = (TelephonyManager) ScheduleApplication.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getSubscriberId();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param dpValue
     * @return
     */
    public static int dip2px(float dpValue) {
        final float scale = ScheduleApplication.applicationContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     *
     * @param pxValue
     * @return
     */
    public static int px2dip(float pxValue) {
        final float scale = ScheduleApplication.applicationContext.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 获取状态栏高度
     *
     * @param activity
     * @return
     */
    public static int getStatusHeight(Activity activity){
        int statusHeight = 0;
        Rect localRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight){
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
                statusHeight = activity.getResources().getDimensionPixelSize(i5);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

    /**
     * sim卡是否可用
     *
     * @return
     */
    public static boolean canUseSim() {
        try {
            TelephonyManager mgr = (TelephonyManager) ScheduleApplication.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);

            return TelephonyManager.SIM_STATE_READY == mgr
                    .getSimState();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
